//
//  SCEatPlayController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/7/1.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCEatPlayController.h"
#import "SCEatPlayCell.h"
#import "SCEatPlayStoreDetailController.h"
#import "MoreDropDownMenu.h"

@interface SCEatPlayController ()<UITableViewDelegate,UITableViewDataSource,MoreDropDownMenuDataSource,MoreDropDownMenuDelegate>

@property (weak, nonatomic) IBOutlet UIView      *topBgView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSString         *search_name;
@property (strong, nonatomic) NSMutableArray   *dataArray;
@property (nonatomic, strong) MoreDropDownMenu *menu;
@property (strong, nonatomic) NSArray          *allTitleArray;
@property (strong, nonatomic) NSArray          *nearbyTitleArray;
@property (strong, nonatomic) NSArray          *sifyTitleArray;
@property (strong, nonatomic) NSArray          *allIdArray;
@property (strong, nonatomic) NSArray          *nearbyIdArray;
@property (strong, nonatomic) NSArray          *sifyIdArray;
@property (strong, nonatomic) NSString         *allId;
@property (strong, nonatomic) NSString         *nearbyId;
@property (strong, nonatomic) NSString         *sifyId;

@property (assign, nonatomic) BOOL isMenuShow;
@property (assign, nonatomic) int  pageIndex;

@end

@implementation SCEatPlayController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"吃喝玩乐";
    self.lineView.hidden = NO;
    
    self.tableView.backgroundColor = BGColor;
    self.tableView.rowHeight = 113;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"SCEatPlayCell" bundle:nil] forCellReuseIdentifier:@"SCEatPlayCellID"];
    
    //请求数据
    self.pageIndex = 1;
    self.allId = @"";
    self.nearbyId = @"";
    self.sifyId = @"";
    [self requestData];
    
    // 添加尾部刷新
    [self addHeaderFooterRefreshing];
}
#pragma mark - 加载/刷新
- (void)addHeaderFooterRefreshing{
    
    self.tableView.mj_header = [SCRefreshMyHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self requestData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.pageIndex++;
        [self requestData];
    }];
}
#pragma mark - 表格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SCEatPlayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCEatPlayCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SCModel *model = self.dataArray[indexPath.row];
    SCEatPlayStoreDetailController *vc = [[SCEatPlayStoreDetailController alloc]init];
    vc.shop_id = model.store_id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 请求数据
- (void)requestData{
    
    NSString *url = [MyTool splicingWithUrl:@"admin.php?g=System&c=EdphApp&a=index"];
    NSDictionary *dict = @{@"edph_name":@"",
                           @"type_id":self.sifyId,
                           @"nearby":self.nearbyId,
                           @"long":@"",
                           @"lat":@"",
                           @"all_id":self.allId,
                           @"pagenow":@(self.pageIndex),
                           @"size":@"10"};
    //NSLog(@"提交的数据：%@",dict);
    if (self.pageIndex == 1) {
        [SVProgressHUD showWithStatus:@"加载中..."];
    }
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        
        //NSLog(@"返回的数据%@",result);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
        if (self.pageIndex == 1) {
            [self.dataArray removeAllObjects];
        }
        
        NSArray *tempArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"shop_info"]];
        [self.dataArray addObjectsFromArray:tempArray];
        [self.tableView reloadData];
        if (tempArray.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        if (self.isMenuShow != YES) {
            self.allIdArray = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"all"]];
            [self setMenuView];
        }
    }];
}
#pragma mark - 点击上面筛选条件
- (void)setMenuView{
    
    NSMutableArray *mut = [NSMutableArray array];
    [mut addObject:@"全部"];
    for (SCModel *model in self.allIdArray) {
        [mut addObject:model.cat_name];
    }
    self.allTitleArray = [mut copy];
    self.nearbyTitleArray = @[@"附近",@"0-1km",@"0-3km",@"0-5km",@"0-10km"];
    self.sifyTitleArray = @[@"筛选",@"距离最近",@"价格",@"销量"];
    
    self.nearbyIdArray = @[@"1",@"3",@"5",@"10"];
    self.sifyIdArray = @[@"1",@"2",@"3"];
    
    self.menu = [[MoreDropDownMenu alloc] initWithOrigin:CGPointMake(0, 55) andHeight:40];
    self.menu.delegate = self;
    self.menu.dataSource = self;
    //当下拉菜单收回时的回调，用于网络请求新的数据
    [self.view addSubview:self.menu];
    //[self.menu selectIndexPath:[MoreIndexPath indexPathWithCol:1 row:1]];
    //展示了菜单
    self.isMenuShow = YES;
}
#pragma mark - MoreDropDownMenuDataSource and MoreDropDownMenuDelegate
- (NSInteger)numberOfColumnsInMenu:(MoreDropDownMenu *)menu{
    return 3;
}
- (NSInteger)menu:(MoreDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column{
    if (column == 0) {
        return self.allTitleArray.count;
    }else if (column == 1){
        return self.nearbyTitleArray.count;
    }else {
        return self.sifyTitleArray.count;
    }
}
- (NSString *)menu:(MoreDropDownMenu *)menu titleForRowAtIndexPath:(MoreIndexPath *)indexPath{
    if (indexPath.column == 0) {
        return self.allTitleArray[indexPath.row];
    } else if (indexPath.column == 1){
        return self.nearbyTitleArray[indexPath.row];
    } else {
        return self.sifyTitleArray[indexPath.row];
    }
}
- (NSArray *)menu:(MoreDropDownMenu *)menu arrayForRowAtIndexPath:(MoreIndexPath *)indexPath{
    if (indexPath.column == 0) {
        return self.allTitleArray;
    } else if (indexPath.column == 1){
        return self.nearbyTitleArray;
    } else {
        return self.sifyTitleArray;
    }
}
- (void)menu:(MoreDropDownMenu *)menu didSelectRowAtIndexPath:(MoreIndexPath *)indexPath{
    if (indexPath.column == 0) {
        if (indexPath.row == 0) {
            self.allId = @"";
        }else{
            SCModel *model = self.allIdArray[indexPath.row-1];
            self.allId = model.cat_id;
        }
    }else if (indexPath.column == 1){
        if (indexPath.row == 0) {
            self.nearbyId = @"";
        }else{
            self.nearbyId = self.nearbyIdArray[indexPath.row-1];
        }
    }else{
        if (indexPath.row == 0) {
            self.sifyId = @"";
        }else{
            self.sifyId = self.sifyIdArray[indexPath.row-1];
        }
    }
    //NSLog(@"点击了第%ld个: %ld 项目",indexPath.column,indexPath.row);
    self.pageIndex = 1;
    [self requestData];
}


@end
